<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane v-for="item in tabsList" :key="item.name" :label="item.label" :name="item.name"></el-tab-pane>
    </el-tabs>

    <el-row :gutter="10" class="mb20">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['enterprise:workOrderPerson:add']"
        >添加{{ workOrderPersonType.label}}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item :label="workOrderPersonType.label+'姓名'">
        <el-input v-model="queryParams.workOrderPersonName" placeholder="请输入" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="workOrderPersonType.label+'手机号'">
        <el-input v-model="queryParams.workOrderPersonPhone" placeholder="请输入" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接单时间">
        <el-date-picker v-model="dateRange" type="daterange" range-separator="~" start-placeholder="开始日期"
                        end-placeholder="结束日期" style="width: 240px"
                        value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" key="lockName" prop="lockName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="所属机构" align="center" key="tenantName" prop="tenantName"
                       :show-overflow-tooltip="true"
      />
      <el-table-column :label="workOrderPersonType.label+'名称'" align="center" key="workOrderPersonName" prop="workOrderPersonName"
                       :show-overflow-tooltip="true"
      />
      <el-table-column :label="workOrderPersonType.label+'手机号'" align="center" key="workOrderPersonPhone" prop="workOrderPersonPhone"
                       :show-overflow-tooltip="true"
      />
      <el-table-column label="工单数量" align="center" prop="orderCount"></el-table-column>
      <el-table-column label="最新接单时间" align="center" prop="latestOrderTime">
        <template slot-scope="scope">
          <span>{{ scope.row.latestOrderTime|formatDate('YYYY-MM-DD HH:mm') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />

    <!--  添加维修员弹出层   -->
    <Add v-model="open" :title="title" :workOrderPersonType="workOrderPersonType" @success="getList"/>
  </div>
</template>

<script>
import { listWorkOrderPerson, deleteWorkOrderPerson } from '@/api/houseManagement/workOrderPerson'
import Add from './components/add.vue'
import { debounce } from '@/utils'

export default {
  name: 'Index',
  components: {
    Add
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workOrderPersonName: undefined,// 工单人员名称
        workOrderPersonPhone: undefined,// 工单人员手机号码
        workOrderPersonType: undefined,// 工单人员类型：1.维修员；2.保洁员；3.带看员；
        time: undefined   // 接单时间
      },
      activeName: 'maintenance',
      tabsList: [
        {
          label: '维修员',
          name: 'maintenance',
          roleKey:'maintenance',
          value: '1'
        },
        {
          label: '带看员',
          name: 'attendant',
          roleKey: 'attendant',
          value: '3'
        },
        {
          label: '保洁员',
          name: 'cleaner',
          roleKey: 'cleaner',
          value: '2'
        },
      ]
    }
  },
  computed:{
    /** 工单人员类型 */
    workOrderPersonType() {
      return this.tabsList.find(item => item.name === this.activeName)
    }
  },
  watch: {},
  created() {
    this.initData()
  },
  methods: {
    /** 初始化数据 */
    initData() {
      this.activeName = this.tabsList[0].name
      this.dateRange = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        workOrderPersonName: undefined,
        workOrderPersonPhone: undefined,
        workOrderPersonType: this.tabsList[0].value,
        time: undefined
      }
      this.getList()
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listWorkOrderPerson(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        workOrderPersonName: undefined,
        workOrderPersonPhone: undefined
      }
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleClick(tab, event) {
      console.log(tab, event)
      this.activeName = tab.name
      this.dateRange = []
      this.queryParams.workOrderPersonName = undefined
      this.queryParams.workOrderPersonPhone = undefined
      this.queryParams.workOrderPersonType = this.tabsList.find(item => item.name === tab.name).value
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加'+ this.workOrderPersonType.label
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('此操作将删除该'+ this.workOrderPersonType.label + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteWorkOrderPerson(row.workOrderPersonId).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
